package com.jason.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.jason.system.common.ResultEntity;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.AdminRole;
import com.jason.system.entity.User;
import com.jason.system.service.RoleService;
import com.jason.system.service.UserService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/1 21:27
 */

@RestController
@RequestMapping("/api/role")
@Api(value = "/role",tags = "角色接口")
public class RoleController {

  private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

  @Autowired
  RoleService roleService;

  @Autowired
  UserService userService;

  /**
   * 获取所有角色信息
   * @return JSONObject
   */
  @ApiOperation(value = "获取所有角色",notes = "获取所有角色类别信息")
  @ApiResponses({
          @ApiResponse(code = 200,message = "获取所有角色类别成功"),
          @ApiResponse(code = 201,message = "获取所有角色类别失败")
  })
  @GetMapping("/getAll")
  @RequiresRoles("role:root")
  public JSONObject getAll(){
    logger.info("==>>>RoleController==>>>进行路径：/api/role/getAll.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    List<AdminRole> roleList = roleService.getAllRole();
    logger.info("==>>>获取所有角色信息==>>>: {}",roleList);
    if (roleList!=null) {
      r.setCode(200);
      r.setMessage("获取所有角色类别成功");
      r.setData(roleList);
    }else {
      r.setCode(201);
      r.setMessage("获取所有角色类别失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  /**
   * 获取用户与角色的关系
   * @return JSONObject
   */
  @ApiOperation(value = "获取用户和角色之间的关系",notes = "获取用户和角色之间的关系")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "page", value = "页数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "size",value = "每页个数",required = true,dataType = "Integer")
  })
  @ApiResponses({
          @ApiResponse(code = 200 , message = "获取用户和角色的关系成功"),
          @ApiResponse(code = 201, message = "获取用户和角色的关系失败")
  })
  @RequiresRoles("role:root")
  @GetMapping("/getUserRole")
  public JSONObject getAllUserRole(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
    logger.info("==>>>RoleController==>>>进行路径：/api/role/getUserRole.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>RoleController==>>>正在获取用户和角色的关系...");
    Pager<User> users = userService.getAllUser(page,size);
    logger.info("==>>>RoleController==>>>获取用户和角色的关系完成...");
    if (users!=null) {
      r.setCode(200);
      r.setMessage("获取用户和角色的关系成功");
      r.setData(users);
    }else {
      r.setCode(201);
      r.setMessage("获取用户和角色的关系失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value ="添加用户和角色之间的关系",notes = "添加用户和角色之间的关系")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "uId",value = "用户id",required = true,dataType ="Integer"),
          @ApiImplicitParam(paramType = "query",name = "roles",value = "角色id的String集合，以逗号分割",required = true,dataType = "String")
  })
  @ApiResponses({
          @ApiResponse(code = 200 , message = "添加用户和角色的关系成功"),
          @ApiResponse(code = 201, message = "添加用户和角色的关系失败")
  })
  @RequiresRoles("role:root")
  @PostMapping("/insertToUserRole")
  public JSONObject insertToUserRole(String username,Integer rId){
    logger.info("==>>>RoleController==>>>进入路径：/api/role/insertToUserRole.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>RoleController==>>>添加用户和角色的关系中...");
    boolean result = roleService.insertToUserRole(username,rId);
    logger.info("==>>>RoleController==>>>添加用户和角色的关系完成...");
    if (result){
      r.setCode(200);
      r.setMessage("为用户添加角色信息成功");
    }else {
      r.setCode(201);
      r.setMessage("为用户添加角色信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value ="删除用户和角色之间的关系",notes = "删除用户和角色之间的关系")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "uId",value = "用户id",required = true,dataType ="Integer"),
          @ApiImplicitParam(paramType = "query",name = "roles",value = "角色id的String集合，以逗号分割",required = true,dataType = "String")
  })
  @ApiResponses({
          @ApiResponse(code = 200 , message = "删除用户和角色的关系成功"),
          @ApiResponse(code = 201, message = "删除用户和角色的关系失败")
  })
  @RequiresRoles("role:root")
  @PostMapping("/deleteUserRole")
  public JSONObject deleteUserRole(@RequestParam("uId") Integer uId, @RequestParam("roles") String roles){
    logger.info("==>>>RoleController==>>>进入路径：/api/role/deleteUserRole.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>RoleController==>>>删除用户和角色的关系中...");
    boolean result = roleService.deleteUserRole(uId,roles);
    logger.info("==>>>RoleController==>>>删除用户和角色的关系完成...");
    if (result){
      r.setCode(200);
      r.setMessage("为用户删除角色信息成功");
    }else {
      r.setCode(201);
      r.setMessage("为用户删除角色信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }
}
